package com.flca.frw.util
{
	import mx.collections.ArrayCollection;
	import mx.controls.Button;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	import mx.controls.DataGrid;
	import mx.controls.listClasses.ListBase;
	import com.flca.frw.dto.UserDto;
	import com.flca.frw.dto.RoleDto;
	import flash.display.DisplayObject;
	import mx.core.UIComponent;
	import mx.core.Container;
	import mx.controls.TextInput;
	import mx.controls.TextArea;
	import mx.controls.CheckBox;
	import mx.controls.ComboBox;
	import mx.containers.FormItem;
	import mx.controls.Label;
	
	public class GuiUtil 
	{
		public static function getRoleNameAsArray(aUser:UserDto):Array
		{
			var role:RoleDto;
			var result:Array;
			
			result = new Array();
			
			if (aUser != null && aUser.roles != null)
			{
				for (var i:int; i < aUser.roles.length; i++)
				{
					role = aUser.roles[i] as RoleDto;
					result.push(role.role);
				}
			}
			
			return result;
		}

		public static function getRoleNameAsString(aUser:UserDto):String
		{
			var result:String = "";
			var arr:Array = getRoleNameAsArray(aUser);
			for (var i:int; i < arr.length; i++) {
				var role:String = arr[i].toString();
				result = result + role;
				if (i < arr.length - 1) {
					result = result + ",";
				}
			}
			return result;
		}
		
		public static function makeRolesFromNames(aRoleNames:Array, aRoles:Array):Array
		{
			var role:RoleDto;
			var matchingRole:RoleDto;
			var roleName:String;
			var result:Array;
			
			result = new Array();
			
			if (aRoleNames != null)
			{
				for (var i:int; i < aRoleNames.length; i++)
				{
					role = null;
					roleName = aRoleNames[i] as String;
					
					for (var j:int; j < aRoles.length; j++)
					{
						matchingRole = aRoles[j] as RoleDto;
						if (matchingRole.role == roleName)
						{
							role = matchingRole;
							break;
						}
					}
					
					if (role == null)
					{
						role = new RoleDto();
					}
					
					role.role = roleName;
					
					result.push(role);
				}
			}
			
			return result;
		}
		
	 	public static function copyArray(aArray:Array):Array
	 	{
	 		var result:Array;
	 		
	 		if (aArray != null)
	 		{
	 			result = new Array(aArray.length);
	 			for (var i:int = 0; i < aArray.length; i++)
	 			{
	 				result[i] = aArray[i];
	 			}
	 		}
	 		else
	 		{
	 			result = new Array();
	 		}
	 		
	 		return result;
	 	}
	 	
	 	private static var callBackOnConfirm:Function;
	 	
	 	public static function confirmAction(aMessage:String, aCallbackOnConfirm:Function):void
	 	{
	 		callBackOnConfirm = aCallbackOnConfirm;
	 		Alert.show(aMessage, "Confirmation needed", Alert.YES | Alert.NO, null, handleAlertResult, null, Alert.NO);
	 	}
		
		private static function handleAlertResult(aEvent:CloseEvent):void
		{
			if (aEvent.detail == Alert.YES)
			{
				if (callBackOnConfirm != null)
				{
					callBackOnConfirm();
					callBackOnConfirm = null;
				}
			}
			
			return;
		}
		
		public static function updateDataGrid(listbase:ListBase, updatedItem:Object, 
			selectedIndex:int = -1) : void 
		{
			var index:int;
			var arr : Array;
			
			if (listbase.dataProvider is ArrayCollection) {
				arr = (listbase.dataProvider as ArrayCollection).source;
			} else if (listbase.dataProvider is Array) {
				arr = listbase.dataProvider as Array;
			}
			
			if (selectedIndex < 0) {
				index = -1;
				for (var i:int = 0; i < arr.length; i++) {
					var item:Object = arr[i];
					if (item != null && item.equals(updatedItem)) {
						index = i;
						break;
					}
				}
			} else {
				index = selectedIndex;
			}
			
			if (index == -1) {
				arr.push(updatedItem);
				index = arr.length - 1;
			} else {
				arr[index] = updatedItem;
			}
			
			listbase.selectedIndex = -1;
			if (listbase.dataProvider is ArrayCollection) {
				listbase.dataProvider = new ArrayCollection(arr);
			} else if (listbase.dataProvider is Array) {
				listbase.dataProvider = arr;
			}
		}

		public static function removeFromDataGrid(listbase:ListBase, selectedIndex:int) : void 
		{
			if (listbase.dataProvider is ArrayCollection) {
				(listbase.dataProvider as ArrayCollection).removeItemAt(selectedIndex);
			} else if (listbase.dataProvider is Array) {
				//TODO
				//arr = listbase.dataProvider as Array;
			}
/*			
			for (var i:int = 0; i < arr.length; i++) {
				if (i != selectedIndex) {
					updatedArr.push(arr[i]);
				}
			}		

			listbase.selectedIndex = -1;
			if (listbase.dataProvider is ArrayCollection) {
				listbase.dataProvider = new ArrayCollection(updatedArr);
			} else if (listbase.dataProvider is Array) {
				listbase.dataProvider = updatedArr;
			}
*/
		}
		
		public static function showFields(fields:Array) : void {
			showOrHideFields(fields, true);
		}

		public static function hideFields(fields:Array) : void {
			showOrHideFields(fields, false);
		}
		
		public static function showOrHideFields(fields:Array, doShow:Boolean) : void {
			for (var i:int=0; i < fields.length; i++) {
				var fld:UIComponent = fields[i] as UIComponent;
				fld.visible = doShow;
				fld.includeInLayout = doShow;
			}
		}
		
		/**
		 * 
		 */
		public static function clearAllFields(c:Container, clearLabel:Boolean = false) : void 
		{
			var n:int = c.numChildren;
			
			for (var i:int = 0; i < n; i++) {
				var o:DisplayObject = c.getChildAt(i);
				if (o is TextInput) {
					(o as TextInput).text = "";
				} else if (o is TextArea) {
					(o as TextArea).text = "";
				}  else if (o is CheckBox) {
					(o as CheckBox).selected = false;
				} else if (o is ComboBox) {
					(o as ComboBox).selectedIndex = -1;
				}	else if (o is Label && clearLabel) {
					(o as Label).text = "";
				}	else if (o is FormItem) {
					GuiUtil.clearAllFields(o as FormItem, true);//also clear label !
				}  else if (o is Container) {
					GuiUtil.clearAllFields(o as Container);
				}
			}
		}
	}
}